package com.quanyan.place.web;

import com.quanyan.place.entity.UserDemo;
import com.quanyan.place.service.DemoService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对外提供JSON数据格式的api.
 *
 * @author songzj
 * @date 16/4/11-14:03
 */
// 开启REST 风格.
//@RestController
//@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/{id}")
    public UserDemo get(@PathVariable("id") Long id) {
        UserDemo demo = new UserDemo();
        demo.setId(id);
        demo.setName("忍者神龟");
        demoService.registerUser(demo);
        return demo;
    }

    @RequestMapping("/login")
    @Deprecated
    public UserDemo login() {
        String userName = "hugo";
        String password = "hugo";
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
        SecurityUtils.getSubject().login(usernamePasswordToken);
        UserDemo demo = new UserDemo();
        demo.setName("login");
        demoService.registerUser(demo);
        return demo;
    }
    @RequestMapping("/login2")
    public UserDemo login2() {
        String userName = "hugo";
        String password = "hugo";
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
        SecurityUtils.getSubject().login(usernamePasswordToken);
        UserDemo demo = new UserDemo();
        demo.setName("login");
        demoService.registerUser(demo);
        return demo;
    }

    @RequestMapping("/add")
    public UserDemo addUser() {
        UserDemo demo = new UserDemo();
        demo.setName("addUser");
        demo.setId(1l);
        demoService.registerUser(demo);
        return demo;
    }
}
